package com.smalls.newvideotwo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.smalls.newvideotwo.R;
import com.smalls.newvideotwo.db.Album;
import com.smalls.newvideotwo.utils.Constant;
import com.smalls.newvideotwo.utils.ImageLoaderUtil;

import java.util.ArrayList;
import java.util.List;

public class NewVodTypeAdapter extends RecyclerView.Adapter<NewVodTypeAdapter.ViewHolder> {
    private static String TAG = Constant.baddcoolio2tag? Constant.TAGPREFIXSTR + NewVodTypeAdapter.class.getSimpleName():NewVodTypeAdapter.class.getSimpleName();
    private Context context;
    private String curreDate;
    private ImageLoaderUtil imageLoaderUtil = new ImageLoaderUtil();
    private LayoutInflater mInflater;
    public List<Album> vodDatas;
    private View.OnClickListener mOnClickListener = null;
    private View.OnKeyListener mOnKeyListener=null;
    private View.OnFocusChangeListener mOnFocusListener= null;
    private int mPadingCount=0;////补足一页

    static class ViewHolder extends RecyclerView.ViewHolder{
        private FrameLayout broader;
        private ImageView poster;
        private TextView videoName;
        private ImageView shadow;

        public ViewHolder(View itemView) {
            super(itemView);
            poster = (ImageView) itemView.findViewById(R.id.video_poster);
            videoName = (TextView) itemView.findViewById(R.id.video_name);
            broader = (FrameLayout) itemView.findViewById(R.id.broader);
            shadow = (ImageView)itemView.findViewById(R.id.video_poster_shadow);
        }
    }

    public NewVodTypeAdapter(Context context, ArrayList<Album> datas, String vodType,int pcount) {
        this.context = context;
        this.vodDatas = datas;
        this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mPadingCount = pcount;
    }

    @Override
    public NewVodTypeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder;
        View convertView = this.mInflater.inflate(R.layout.movetype_details_item, null);
        holder = new ViewHolder(convertView);
        return holder;
    }

    @Override
    public void onBindViewHolder(final NewVodTypeAdapter.ViewHolder holder, int position) {
        if(position<vodDatas.size()) {
            Album vd = this.vodDatas.get(position);
            holder.poster.setImageResource(R.color.transparent);
            holder.itemView.setTag(position);
            this.imageLoaderUtil.loadImage(vd.getAlbumPic(), holder.poster, this.context, R.mipmap.default_poster, 5.0f);
            if (!TextUtils.isEmpty(vd.getTitle())) {
                holder.videoName.setText(vd.getTitle());
            }
            if(holder.itemView.getVisibility()!=View.VISIBLE) {
                holder.itemView.setVisibility(View.VISIBLE);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Log.i(TAG,"view="+view);
                    if(mOnClickListener!=null) {
                        mOnClickListener.onClick(view);
                    }
                }
            });


            holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    Log.i(TAG,"onFocusChange b="+b+" v.h="+view.getHeight()+" v="+view);
                    if(b) {
                        holder.shadow.setVisibility(View.VISIBLE);
                        Animation scaleAnimation = AnimationUtils.loadAnimation(context, R.anim.dialog_exit_btn_scale);
                        scaleAnimation.setFillAfter(true);
                        view.startAnimation(scaleAnimation);
                        view.setSelected(true);
                    } else {
                        holder.shadow.setVisibility(View.GONE);
                        Animation scaleAnimation = AnimationUtils.loadAnimation(context, R.anim.dialog_cancel_btn_scale);
                        scaleAnimation.setFillAfter(true);
                        view.startAnimation(scaleAnimation);
                        view.setSelected(false);
                    }
                    if(mOnFocusListener!=null) {
                        mOnFocusListener.onFocusChange(view,b);
                    }
                }
            });
        } else {
            holder.itemView.setVisibility(View.GONE);
        }
    }

    public long getItemId(int position) {
        return (long) position;
    }

    @Override
    public int getItemCount() {
        if(vodDatas!=null) {
            if(mPadingCount>0) {
                return vodDatas.size()+mPadingCount;
            }
            return vodDatas.size();
        }
        return 0;
    }

    public void setOnClickListenter( View.OnClickListener listenter) {
        mOnClickListener = listenter;
    }

    public void setOnFocusListener(View.OnFocusChangeListener listener) {
        mOnFocusListener = listener;
    }
}
